{{/*
    智能资源处理器 (支持 CSS/JS)
    -----------------------------------
    输入参数:
    - asset: 资源对象 (必须)
    - path: 资源路径 (必须，用于错误处理)
    - jsType: JS 脚本类型  (可选)
    - opts: 配置选项 (可选)
        - minify: 是否压缩 (默认生产环境启用)
        - fingerprint: 是否添加指纹 (默认 false)
        - sourceMap: 是否生成sourceMap (开发环境默认 true)
    返回值: 处理后的资源对象、<link> 标签或 <script> 标签
*/}}

{{/* 参数初始化 */}}
{{ $asset := .asset }}
{{ $path := .path }}
{{ $jsType := .jsType | default "text/javascript"}}
{{ $relPermalink  := $asset.RelPermalink  }}
{{ $mediaType  := $asset.MediaType.Type }}
{{ $opts := merge (dict
    "minify" hugo.IsProduction
    "fingerprint" false
    "sourceMap" (not hugo.IsProduction)
) (.opts | default dict) }}

{{ $runStatus := true}}
{{ $processors := slice }}
{{ $integrity := "" }}

{{/*{{ partial "helpers/log" (dict "message" (printf "生产环境: %v" hugo.IsProduction)) }}*/}}
{{/*{{ partial "helpers/log" (dict "message" (printf "[%s] 🛠️ 处理资源" $path)) }}*/}}

{{/* 参数检查 */}}
{{ if not (isset . "asset") }}
    {{ partial "helpers/log" (dict "level" "ERROR" "message" (printf "📝  缺少 asset 参数")) }}
    {{ $runStatus = false }}
{{ end }}

{{/* 类型检查 */}}
{{  $css := resources.Get $relPermalink }}
{{ if not $css }}
    {{ partial "helpers/log" (dict "level" "ERROR" "message" (printf "[%s] ❌  资源对象缺失: %s" $path $path)) }}
    {{ $runStatus = false }}
{{/*{{ else }}*/}}
{{/*    {{ partial "helpers/log" (dict "message" (printf "[%v] 资源加载成功 (%v - %v)" $path $mediaType $relPermalink)) }}*/}}
{{ end }}

{{/* 根据文件类型设置处理器 */}}
{{ if $runStatus }}
    {{ with $mediaType }}
        {{/* 类型检查 */}}
        {{ if eq . "text/javascript" }}
            {{/* JS 处理 */}}
            {{ if $opts.sourceMap }}
                {{/* 开发环境不做任何处理 */}}
                {{ if not hugo.IsDevelopment }}
                    {{ $asset = $asset | js.Build (dict "sourceMap" "inline") }}
                {{ end }}
            {{ else }}
                    {{ $asset = $asset | js.Build }}
            {{ end }}

            {{ if $opts.minify }}
                {{ $asset = $asset | minify }}
            {{ end }}

            {{ if $opts.fingerprint }}
                {{ $asset = $asset | fingerprint }}
            {{ end }}

            {{ if $opts.fingerprint }}
                {{ $integrity = printf ` integrity="%s" crossorigin="anonymous"` $asset.Data.Integrity }}
            {{ end }}
            {{ if eq $jsType "module" }}
                <script type="module" src="{{ $asset.RelPermalink }}"{{ $integrity | safeHTML }}></script>
            {{ else if eq $jsType "text/javascript"  }}
                <script type='text/javascript' src="{{ $asset.RelPermalink }}"{{ $integrity | safeHTML }}></script>
            {{ else }}
                {{ partial "helpers/log" (dict "level" "ERROR" "message" (printf "🔧  不支持的 JS 脚本类型: %s" $jsType)) }}
            {{ end }}

        {{ else if eq . "text/css" }}
            {{/* CSS 处理 */}}
            {{ if $opts.sourceMap }}
                {{/* 开发环境不做任何处理 */}}
                {{ if not hugo.IsDevelopment }}
                    {{ $asset = $asset | postCSS (dict "noMap" false) }}
                {{ end }}
            {{ else }}
                {{ $asset = $asset | postCSS }}
            {{ end }}

            {{ if $opts.minify }}
                {{ $asset = $asset | minify }}
            {{ end }}

            {{ if $opts.fingerprint }}
                {{ $asset = $asset | fingerprint }}
            {{ end }}

            {{ if $opts.fingerprint }}
                {{ $integrity = printf ` integrity="%s" crossorigin="anonymous"` $asset.Data.Integrity }}
            {{ end }}

            <link rel="stylesheet" href="{{ $asset.RelPermalink }}"{{ $integrity | safeHTML }}>
        {{ else }}
            {{ partial "helpers/log" (dict "level" "ERROR" "message" (printf "🔧  不支持的媒体类型: %s" .)) }}
        {{ end }}
    {{ end }}
{{ end }}